package com.hruiworks.campusgroup.controller;

import com.hruiworks.campusgroup.common.BaseResponse;
import com.hruiworks.campusgroup.common.PageVO;
import com.hruiworks.campusgroup.enums.SuccessCode;
import com.hruiworks.campusgroup.manager.entity.GroupFullInfo;
import com.hruiworks.campusgroup.pojo.entity.Group;
import com.hruiworks.campusgroup.pojo.entity.User;
import com.hruiworks.campusgroup.pojo.query.GroupPageQuery;
import com.hruiworks.campusgroup.pojo.request.GroupCreateRequest;
import com.hruiworks.campusgroup.service.GroupService;
import com.hruiworks.campusgroup.utils.AuthorityUtils;
import com.hruiworks.campusgroup.utils.ResultUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 组团Controller
 *
 * @author jiushui
 */
@RestController
@RequestMapping("/api/group")
public class GroupController {

    @Resource
    private GroupService groupService;


    /**
     * 创建团队
     * @param groupCreateRequest 团队创建请求体
     * @param httpServletRequest http请求体
     * @return 创建成功返回新建的团队的id
     */
    @PostMapping("/create")
    private BaseResponse<Long> createGroup(@RequestBody GroupCreateRequest groupCreateRequest, HttpServletRequest httpServletRequest) {
        User user = AuthorityUtils.currentUser(httpServletRequest);
        Long groupId = groupService.createGroup(groupCreateRequest, user);
        return ResultUtils.success(groupId, SuccessCode.COMMON_SUCCESS);
    }

    /**
     * 加入团队
     * @param groupId 团队id
     * @param httpServletRequest http请求体
     * @return 加入成功返回true
     */
    @GetMapping("/join")
    private BaseResponse<Boolean> joinGroup(@RequestParam("group_id") Long groupId, HttpServletRequest httpServletRequest) {
        User user = AuthorityUtils.currentUser(httpServletRequest);
        Boolean result = groupService.joinGroup(user, groupId);
        return ResultUtils.success(result, SuccessCode.COMMON_SUCCESS);
    }


    /**
     * 无特殊条件的团队分页
     * @param groupPageQuery 分页查询条件
     * @return 分页结果
     */
    @PostMapping("/page")
    private BaseResponse<PageVO<Group>> listPageGroup(@RequestBody GroupPageQuery groupPageQuery) {
        PageVO<Group> groupPageVO = groupService.listGroupPage(groupPageQuery);
        return ResultUtils.success(groupPageVO, SuccessCode.COMMON_SUCCESS);
    }

    /**
     * 针对个人的活动分页
     * 额外限制，团队列表中不出现自己已经加入、创建、过期的队伍
     * @param groupPageQuery 活动分页查询条件
     * @param httpServletRequest http请求体
     * @return 分页结果
     */
    @PostMapping("/page/main")
    private BaseResponse<PageVO<Group>> listPageGroupMain(@RequestBody GroupPageQuery groupPageQuery, HttpServletRequest httpServletRequest) {
        User user = AuthorityUtils.currentUser(httpServletRequest);
        PageVO<Group> groupPageVO = groupService.listGroupPageMain(groupPageQuery, user);
        return ResultUtils.success(groupPageVO, SuccessCode.COMMON_SUCCESS);
    }


    @GetMapping("/page/self")
    private BaseResponse<PageVO<GroupFullInfo>> listPageGroupSelf(@RequestParam("size") Long size,
                                                                  @RequestParam("current") Long current,
                                                                  @RequestParam("created") Integer created,
                                                                  @RequestParam("time") Integer time,
                                                                  HttpServletRequest httpServletRequest) {

        User user = AuthorityUtils.currentUser(httpServletRequest);
        PageVO<GroupFullInfo> groupPageVO = groupService.listGroupPageSelf(size, current, created, time, user);
        return ResultUtils.success(groupPageVO, SuccessCode.COMMON_SUCCESS);
    }

    @GetMapping("/start/change")
    private BaseResponse<Boolean> changeStartTimeNotice(@RequestParam("group_id") Long groupId,
                                                        @RequestParam("time_beforehand") Long timeBeforehand,
                                                        HttpServletRequest httpServletRequest) {
        User user = AuthorityUtils.currentUser(httpServletRequest);
        groupService.changeGroupStartNotice(user, groupId, timeBeforehand);
        return ResultUtils.success(true, SuccessCode.COMMON_SUCCESS);
    }

}
